<?php
/**
 * 接口配置
 * Enter description here ...
 * @author jin
 *
 */
class ControllerMessageActivity extends Controller {
	private $error = array();

	protected function init(){
		$this->load_language('message/activity');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('message/activity');
	}

	public function index() {
		$this->init();
		$this->getList();
	}
	
	public function insert() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_message_activity->addActivity($this->request->post);

			$this->redirectToList();
		}

		$this->getForm();
	}

	public function update() {
		$this->init();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_message_activity->editActivity($this->request->get['activity_id'], $this->request->post);
			$this->redirectToList();
		}

		$this->getForm();
	}

	public function delete() {
		$this->init();

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $article_id) {
				$this->model_message_activity->deleteActivity($article_id);
			}

			$this->redirectToList();
		}

		$this->getList();
	}
	 
	 
	private function getList() {
		
		$this->init();
		$this->load->model('tool/image');
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$data = array(
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);


		$this->data['articles'] = array();
		$total = $this->model_message_activity->getTotalActivitys();
		$results = $this->model_message_activity->getActivitys();
		foreach ($results as $article) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'type' => "edit",
				'href' => $this->url->link('message/activity/update', 'token=' . $this->session->data['token'] . '&activity_id=' . $article['activity_id'] , 'SSL')
			);


			$abPic = "";
			if ($article['pic_url'] && file_exists(DIR_IMAGE . $article['pic_url'])) {
				$abPic = $this->model_tool_image->resize($article['pic_url'], 100, 100);
			} else {
				$abPic = $this->model_tool_image->resize('no_image.jpg', 100, 100);
			}
			$this->data['articles'][] = array(
				'activity_id'=>$article['activity_id'],
				'title'=>$article['title'],
				'description'=>$article['description'],
				'pic_url'=>$article['pic_url'],
				'abPic' =>$abPic,
				'url' =>$article['url'],
				'actions'     => $action,
				'url_type' =>$article['url_type'],
				'date_start' =>$article['date_start'],
				'date_end' =>$article['date_end'],
				'selected'   => isset($this->request->post['selected']) && in_array($article['activity_id'], $this->request->post['selected']),
			);
		}

		$url = "";

		if(isset($this->request->get['activity_id']))
		{
			$url = "&activity_id="+$this->request->get['activity_id'];
		}


		$this->data['insert'] = $this->url->link('message/activity/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('message/activity/delete', 'token=' . $this->session->data['token'].$url, 'SSL');
		
		$this->data['token'] = $this->session->data['token'];
		
		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('message/activity', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();
		
		
		$this->template = 'message/activity_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function getForm() {
		
		$this->load->model('tool/image');
		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}

		if (isset($this->error['error_date'])) {
			$this->data['error_date'] = $this->error['error_date'];
		} else {
			$this->data['error_date'] = '';
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$url=$this->getUrlParameters();

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('message/activity', 'token=' . $this->session->data['token'] , 'SSL'),
      		'separator' => ' :: '
      		);

      		if(isset($this->request->get['activity_id'])){
	      		$this->data['action'] = HTTPS_SERVER . 'index.php?route=message/activity/update&token=' . $this->session->data['token'] . '&activity_id=' . $this->request->get['activity_id'] . $url;
      		}else{
      			$this->data['action'] = HTTPS_SERVER . 'index.php?route=message/activity/insert&token=' . $this->session->data['token'] . '';
      		}

      		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=message/activity&token=' . $this->session->data['token'] . $url;

      		$this->data['token'] = $this->session->data['token'];

      		if (isset($this->request->get['activity_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      			$article_info = $this->model_message_activity->getActivity($this->request->get['activity_id']);
      		}
			if (isset($this->request->post['title'])) {
				$this->data['title'] = $this->request->post['title'];
			} elseif (!empty($article_info)) {
				$this->data['title'] = $article_info['title'];
			} else {
				$this->data['title'] = '';
			}
			
			if (isset($this->request->post['summary'])) {
				$this->data['summary'] = $this->request->post['summary'];
			} elseif (!empty($article_info)) {
				$this->data['summary'] = $article_info['summary'];
			} else {
				$this->data['summary'] = '';
			}
			
			if (isset($this->request->post['limit'])) {
				$this->data['limit'] = $this->request->post['limit'];
			} elseif (!empty($article_info)) {
				$this->data['limit'] = $article_info['limit'];
			} else {
				$this->data['limit'] = 10;
			}
			
			if (isset($this->request->post['used'])) {
				$this->data['used'] = $this->request->post['used'];
			} elseif (!empty($article_info)) {
				$this->data['used'] = $article_info['used'];
			} else {
				$this->data['used'] = 0;
			}
			
			if (isset($this->request->post['date_start'])) {
				$this->data['date_start'] = $this->request->post['date_start'];
			} elseif (!empty($article_info)) {
				$this->data['date_start'] = $article_info['date_start'];
			} else {
				$this->data['date_start'] = '';
			}
			
			if (isset($this->request->post['date_end'])) {
				$this->data['date_end'] = $this->request->post['date_end'];
			} elseif (!empty($article_info)) {
				$this->data['date_end'] = $article_info['date_end'];
			} else {
				$this->data['date_end'] = '';
			}
			
			
			if (isset($this->request->post['description'])) {
				$this->data['description'] = $this->request->post['description'];
			} elseif (!empty($article_info)) {
				$this->data['description'] = $article_info['description'];
			} else {
				$this->data['description'] = '';
			}
			
			if (isset($this->request->post['url'])) {
				$this->data['url'] = $this->request->post['url'];
			} elseif (!empty($article_info)) {
				$this->data['url'] = $article_info['url'];
			} else {
				$this->data['url'] = '';
			}
			
			if (isset($this->request->post['ur_type'])) {
				$this->data['url_type'] = $this->request->post['url_type'];
			} elseif (!empty($article_info)) {
				$this->data['url_type'] = $article_info['url_type'];
			} else {
				$this->data['url_type'] = '';
			}
			
			if (isset($this->request->post['pic_url'])) {
				$this->data['pic_url'] = $this->request->post['pic_url'];
				if ($this->data['pic_url'] && file_exists(DIR_IMAGE . $this->data['pic_url'])) {
					$this->data['abPic'] = $this->model_tool_image->resize($this->data['pic_url'], 100, 100);
				} else {
					$this->data['abPic']  = $this->model_tool_image->resize('no_image.jpg', 100, 100);
				}
			} elseif (!empty($article_info)) {
				$this->data['pic_url'] = $article_info['pic_url'];
				if ($article_info['pic_url'] && file_exists(DIR_IMAGE . $article_info['pic_url'])) {
					$this->data['abPic'] = $this->model_tool_image->resize($article_info['pic_url'], 100, 100);
				} else {
					$this->data['abPic']  = $this->model_tool_image->resize('no_image.jpg', 100, 100);
				}
			} else {
				$this->data['pic_url'] = '';
				$this->data['abPic']  = $this->model_tool_image->resize('no_image.jpg', 100, 100);
			}
			$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
      		
			$this->template = 'message/activity_form.tpl';
			$this->children = array(
				'common/header',
				'common/footer'
			);
			$this->response->setOutput($this->render());
      		
	}
	

	private function validateForm() {
		if (!$this->error) {
			return TRUE;
		} else {
			$this->error['warning'] = $this->language->get('error_required_data');
				
			return FALSE;
		}
	}

	private function validateDelete() {
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}


	private function redirectToList(){
		$this->session->data['success'] = sprintf($this->language->get('text_success'),$this->language->get('heading_title'));

		$url=$this->getUrlParameters();

		$this->redirect(HTTPS_SERVER . 'index.php?route=message/activity&token=' . $this->session->data['token'] . $url);
	}
	 
	private function getCommonUrlParameters(){
		$url = '';

		if (isset($this->request->get['filter_title'])) {
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['filter_date_add'])) {
			$url .= '&filter_date_add=' . $this->request->get['filter_date_add'];
		}

		if (isset($this->request->get['filter_article_category_id'])) {
			$url .= '&filter_article_category_id=' . $this->request->get['filter_article_category_id'];
		}

		return $url;
	}
	 
	public function getUrlParameters(){
		$url = '';

		$url=$this->getCommonUrlParameters();

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		return $url;
	}
	
	
		public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			$this->load->model('message/article');

			$data = array(
				'filter_title' => $this->request->get['filter_name'],
				'start'       => 0,
				'limit'       => 100
			);

			$results = $this->model_message_article->getArticles($data);

			foreach ($results as $result) {
				$json[] = array(
					'news_article_id' => $result['news_article_id'], 
					'title'        => strip_tags(html_entity_decode($result['title'], ENT_QUOTES, 'UTF-8'))
				);
			}		
		}

		$sort_order = array();

		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['title'];
		}

		array_multisort($sort_order, SORT_ASC, $json);

		$this->response->setOutput(json_encode($json));
	}		

}
?>